package com.xinda.admin.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xinda.admin.pojo.Member;
import com.xinda.admin.pojo.Region;
import com.xinda.admin.service.MemberService;
import com.xinda.admin.vo.OrderEditVo;
/*
 * 普通用户：买家
 */
@Controller
public class MemberController {

	@Resource
	private MemberService memberService;
	
/*	
	 * 普通用户列表
	 */
	@RequestMapping(value = "userList")
	public String memberList(Model model,HttpSession httpSession,String memberName,String datemin,String datemax) {
		
		Map<String,String> conditionsMap = new HashMap<String,String>();
		conditionsMap.put("memberName", memberName);
		conditionsMap.put("datemin", datemin);
		conditionsMap.put("datemax", datemax);	
		
		List<Member> memberList = new ArrayList<Member>();
		memberList = memberService.selectAllMember(conditionsMap);
				
		
		int countMember = memberService.countMember();
		model.addAttribute("countMember", countMember);
		model.addAttribute("memberList", memberList);
		model.addAttribute("memberName", memberName);
		model.addAttribute("datemin",datemin);
		model.addAttribute("datemax", datemax);
		return "admin/user/userList";
	}

	/*
	 * 添加用户页面
	 */
	@RequestMapping(value = "userAdd")
	public String memberAdd(Member member,Model model) {
		Date registerTime = new Date();
		member.setRegisterTime(registerTime);
		List<Region> provincelist = memberService.queryRegion(0);
		model.addAttribute("provincelist", provincelist);
		return "admin/user/userAdd";
	}
	
	/*添加用户*/
	@RequestMapping(value = "insertMember")
	@ResponseBody
	public String insertMember(Model model ,Member member) {
		Date registerTime = new Date();
		member.setRegisterTime(registerTime);
		memberService.insertMember(member);
		return  "";
	}	

	/*
	 * 已停用用户记录
	 */
	@RequestMapping(value = "userDeactivate")
	public String memberDeactivate(Model model) {
		List<Member> memberDeactivate = new ArrayList<Member>();
		memberDeactivate =memberService.selectStatusMember1();
		for(int i=0;i<memberDeactivate.size();i++){
		}
		model.addAttribute("memberDeactivate", memberDeactivate);
		return "admin/user/userDeactivate";
	}
	/*
	 * 已停用用户
	 */
	@RequestMapping(value="memberStop")
	@ResponseBody
	public String memberStop(int id){
		
		memberService.stopMmeber(id);
		return"";
		
	}
	/*
	 * 启用用户
	 * */
	@RequestMapping(value="memberStart")
	@ResponseBody
	public String memberStart(int id){
		
		memberService.startMember(id);
		return"";
		
	}
	/*
	 * 查看用户
	 */
	@RequestMapping(value = "memberShow")
	public String memberShow(int id,Model model) {
		Member member = memberService.selectmemberShow(id);
		
		model.addAttribute("member", member);
		return "admin/user/userShow";
		
	}
	
	/*
	 * 用户订单详情
	 */
	@RequestMapping(value = "userBuyRecords")
	public String memberOrder(int id, Model model) {
		
		List<OrderEditVo> orderList = memberService.OrderMember(id);
		
		model.addAttribute("count", orderList.size());
		model.addAttribute("orderList", orderList);
		
		return "admin/user/userBuyRecords";
	}
	/*
	 * 	删除用户
	 * */
	@RequestMapping(value = "memberDel")
	@ResponseBody
	public void memberDel(int id){
		memberService.deletememberById(id);
	}
	/*
	 * 批量删除用户
	 * */
	@RequestMapping(value="membersDel")
	@ResponseBody
	public String membersDel(String id){
		String[] ids=id.split(",");
		memberService.deletemembersById(ids);
		return "";
	}
	
	

	
}
